package com.atguigu.system.manager;

import com.atguigu.common.utils.BeanUtils;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.vo.index.RouterVo;
import com.atguigu.model.vo.menu.SysMenuQueryTreeRespVo;
import com.atguigu.system.constant.SysMenuTypeEnum;
import com.atguigu.system.constant.SysUserStatusEnum;
import com.atguigu.system.mapper.SysMenuMapper;
import com.atguigu.system.utils.MenuHelper;
import com.atguigu.system.utils.RouterHelper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author flyduck
 * @since 2023-10-25
 */
@Service
public class SysMenuManager extends ServiceImpl<SysMenuMapper, SysMenu> {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 根据parentId查询
     * @param parentId
     * @return
     */
    public List<SysMenu> findByParentId(Long parentId) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        return sysMenuMapper.selectList(queryWrapper);
    }

    /**
     * 根据parentId查询
     * @param parentId
     * @return
     */
    public Integer countByParentId(Long parentId) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        return sysMenuMapper.selectCount(queryWrapper);
    }

    /**
     * 根据状态查询
     * @param status
     * @return
     */
    public List<SysMenu> findByStatus(int status) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        return sysMenuMapper.selectList(queryWrapper);
    }

    /**
     * 根据状态查询
     * @param status
     * @return
     */
    public List<SysMenu> findByStatusOrderBySortValueAsc(int status) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        queryWrapper.orderByAsc("sort_value");
        return sysMenuMapper.selectList(queryWrapper);
    }

    /**
     * 根据userId获取菜单权限
     * @param userId
     * @return
     */
    public List<RouterVo> findRouterVoListByUserId(Long userId) {
        //根据用户id查询菜单
        List<SysMenu> sysMenuList = findByUserIdOrderBySortValueAsc(userId);

        //构建树形结构
        List<SysMenuQueryTreeRespVo> treeRespVos = BeanUtils.toBeanList(sysMenuList, SysMenuQueryTreeRespVo.class);
        List<SysMenuQueryTreeRespVo> treeList = MenuHelper.buildTree(treeRespVos);

        //转化为前端路由的格式
        List<RouterVo> routerVoList = RouterHelper.buildRouters(treeList);
        return routerVoList;
    }

    /**
     * 根据用户id查询菜单
     * @param userId
     * @return
     */
    public List<SysMenu> findByUserIdOrderBySortValueAsc(Long userId) {
        if (1 == userId) {
            //如果用户id为1，查询所有权限数据
            return this.findByStatusOrderBySortValueAsc(SysUserStatusEnum.NORMAL.getCode());
        }else {
            //如果用户id不是1，根据用户id查询菜单权限
            return sysMenuMapper.findByUserIdOrderBySortValueAsc(userId);
        }
    }

    /**
     * 根据userId获取按钮权限
     * @param userId
     * @return
     */
    public List<String> findUserPermsListByUserId(Long userId) {
        //根据用户id查询菜单
        List<SysMenu> sysMenuList = findByUserIdOrderBySortValueAsc(userId);

        List<String> permissionList = sysMenuList.stream()
                .filter(sysMenu -> SysMenuTypeEnum.BUTTON.getCode().equals(sysMenu.getType()))
                .map(SysMenu::getPerms)
                .collect(Collectors.toList());

        return permissionList;
    }
}
